Import mojo

Import diddy
Import diddy.functions
Import numberInputButton
Import tween2

Class MultiChoicePanel
	Const NO_ANSWER:Int = -1
	

	
	Field x:float
	Field y:float
	Field width
	Field height
	Field alpha:Float = 1
	Field maxChoices:Int
	Field answer:Int
	Field pressedNumber:Int = NO_ANSWER
	Field buttonsList:ArrayList<NumberInputButton>
	
	Method New(x,y, _width:Int, _height:Int, maxChoices:int)
		Local rowIncrement:Float = _height/maxChoices
		self.x = x
		self.y = y
		self.width = _width
		self.height = _height
		self.maxChoices = maxChoices
 		buttonsList= New ArrayList<NumberInputButton>(maxChoices)
		
		For Local ndx:Int = 1 to maxChoices
			Local number:Int =2
			Local button1:NumberInputButton = NumberInputButton.Create(game.images.Find("ButtonStrip"),x+width/2,(ndx*rowIncrement)-(rowIncrement/2)+y, New MCNumberButton(ndx-1, self), 1, ndx,buttonsList)
			
			button1.highlight=0
		Next

	End
	
	Method NewQuestion(answer:Int)
		Local answerPos:Int = Rnd(0, maxChoices)
		Local ndx2:Int = maxChoices-1
		
		Self.answer = answer
		Print	answer +" "+maxChoices	
		if(answer-maxChoices<0)
			answerPos=maxChoices-1 ' prevent generation of minus number
			Print("answerPos:"+answerPos)
		EndIf
		
		For Local ndx:Int=0 Until maxChoices
			Local val:NumberInputButton = buttonsList.Get(ndx)
			Local newNumber:Int =((answerPos-ndx2)+answer)
			val.setNumber(newNumber)
			val.setState(NumberInputButton.TWEEN_FADE)
			ndx2-=1
		End		
	End
	
	Method Update:void()		
		NumberInputButton.UpdateAll(buttonsList)
	End
	
	Method Render()	
		PushMatrix	
		SetAlpha (0.6)
		SetColor(200,0,0)
		DrawRect(x,y, width, height )
		
		SetColor(255,255,255)
		NumberInputButton.DrawAll(buttonsList)
		PopMatrix
	End
End

Class MCNumberButton Extends ButtonEvent
	Field ndx:Int
	Field parent:MultiChoicePanel
	
	Method New(ndx:Int, parent:MultiChoicePanel)
		Self.ndx=ndx
		Self.parent = parent
	End
	
	Method OnClicked:Void()
	parent.pressedNumber=parent.buttonsList.Get(ndx).GetNumber()
	End
End